﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

using MyVideoManager2.DataModel.simple;



namespace MyVideoManager2.Data
{
    [Windows.Foundation.Metadata.WebHostHidden]
    public class Dossier : MyVideoManager2.Common.BindableBase
    {
        private static Uri _baseUri = new Uri("ms-appx:///");


       

        public Dossier()
        {
        }

        public Dossier(String nomDossier, String uniqueId, String imagePath, String cheminDossier)
        {
            this._uniqueId = uniqueId;
            this._nomDossier = nomDossier;
            this._imagePath = imagePath;
            this._cheminDossier = cheminDossier;
            Items.CollectionChanged += ItemsCollectionChanged;
        }
        private string _cheminDossier = string.Empty;
        public string CheminDossier
        {
            get { return (this._cheminDossier); }
            set { this.SetProperty(ref this._cheminDossier, value); }
        }

        private Peripherique _peripherique;
        public Peripherique GetPeripherique()
        {
            return (this._peripherique);
        }
        public void SetPeripherique(Peripherique value)
        {
            this._peripherique = value;
        }


        private string _uniqueId = string.Empty;
        public string UniqueId
        {
            get { return this._uniqueId; }
            set { this.SetProperty(ref this._uniqueId, value); }
        }

        private string _nomDossier = string.Empty;
        public string NomDossier
        {
            get { return (this._nomDossier); }
            set { this.SetProperty(ref this._nomDossier, value); }
        }

        private ImageSource _image = null;
        private string _imagePath = string.Empty;
        public Uri ImagePath
        {
            get { return new Uri(Dossier._baseUri, this._imagePath); }
            set { this._imagePath=string.Format("{0}",value); }
        }
        
        public ImageSource Image
        {
            get
            {
                if (this._image == null && this._imagePath != null)
                {
                    this._image = new BitmapImage(new Uri(Dossier._baseUri, this._imagePath));
                }
                return this._image;
            }

            set
            {
                this._imagePath = null;
                this.SetProperty(ref this._image, value);
            }
        }

        public void SetImage(String path)
        {
            this._image = null;
            this._imagePath = path;
            this.OnPropertyChanged("Image");
        }
        

        public override string ToString()
        {
            return this.NomDossier;
        }

        private void ItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // Fournit un sous-ensemble de la collection complète d'éléments avec laquelle effectuer une liaison à partir d'un GroupedItemsPage
            // pour deux raisons : GridView ne virtualise pas les collections d'éléments volumineuses, et
            // améliore l'expérience utilisateur lors de la navigation dans des groupes contenant un nombre important
            // d'éléments.
            //
            // 12 éléments maximum sont affichés, car cela se traduit par des colonnes de grille remplies,
            // qu'il y ait 1, 2, 3, 4 ou 6 lignes affichées

            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    if (e.NewStartingIndex < 12)
                    {
                        TopItems.Insert(e.NewStartingIndex,Items[e.NewStartingIndex]);
                        if (TopItems.Count > 12)
                        {
                            TopItems.RemoveAt(12);
                        }
                    }
                    break;
                case NotifyCollectionChangedAction.Move:
                    if (e.OldStartingIndex < 12 && e.NewStartingIndex < 12)
                    {
                        TopItems.Move(e.OldStartingIndex, e.NewStartingIndex);
                    }
                    else if (e.OldStartingIndex < 12)
                    {
                        TopItems.RemoveAt(e.OldStartingIndex);
                        TopItems.Add(Items[11]);
                    }
                    else if (e.NewStartingIndex < 12)
                    {
                        TopItems.Insert(e.NewStartingIndex, Items[e.NewStartingIndex]);
                        TopItems.RemoveAt(12);
                    }
                    break;
                case NotifyCollectionChangedAction.Remove:
                    if (e.OldStartingIndex < 12)
                    {
                        TopItems.RemoveAt(e.OldStartingIndex);
                        if (Items.Count >= 12)
                        {
                            TopItems.Add(Items[11]);
                        }
                    }
                    break;
                case NotifyCollectionChangedAction.Replace:
                    if (e.OldStartingIndex < 12)
                    {
                        TopItems[e.OldStartingIndex] = Items[e.OldStartingIndex];
                    }
                    break;
                case NotifyCollectionChangedAction.Reset:
                    TopItems.Clear();
                    while (TopItems.Count < Items.Count && TopItems.Count < 12)
                    {
                        TopItems.Add(Items[TopItems.Count]);
                    }
                    break;
            }
        }

        private ObservableCollection<Film> _items = new ObservableCollection<Film>();
        public ObservableCollection<Film> Items
        {
            get { return this._items; }
        }

        private ObservableCollection<Film> _topItem = new ObservableCollection<Film>();
        public ObservableCollection<Film> TopItems
        {
            get {return this._topItem; }
        }
    }
}
